$(function(){
	
	// 日期控件
	laydate.render({
	  elem: '#riqi' ,//指定元素
	  min: '0',
	  max: '2022-10-7'
	});
	
	

	// 表单验证标识列
	var NameFlag = false;
	var PhoneFlag = false;
	
	
	$("#dingName").blur(function(){
		if($(this).val().length<=0){
			$(this).next().remove();
			$("#dingN").append("<span class='tishi'>请输入您的姓名</span>");
		}else{
			NameFlag = true;
			$(this).next().remove();
		}
	});
	$("#dingShou").blur(function(){
		let thisval = $(this).val();
		if(!(/^1[3456789]\d{9}$/.test(thisval))){
			$(this).next().remove();
			$("#dingS").append("<span class='tishi'>请输入正确的手机号码,手机号码必须为11位数字</span>");
		}else{
			PhoneFlag = true;
			$(this).next().remove();
		}
	});
	
	// 留言改变事件
	var tipsLength = 0;
	$("#liuyanban").bind("input propertychange",function(){
		tipsLength = $(this).val().length;
		$("#liuyanTipsLeft").html(tipsLength);
	});
	
	// 贺卡点击事件
	$(".rightTab>li").click(function(){
		let index = $(this).index();
		$(this).addClass("active1").siblings().removeClass("active1");
		$($(".order-item-right-content").children()[index]).addClass("active2").siblings().removeClass("active2");
	});
	
	// 祝福语点击事件
	$(".order-item-right-pane>li").click(function(){
		$("#liuyanban").val("");
		$("#liuyanban").val($(this).text().trim().replace(/[0-9|.]/g,""));
		tipsLength = $("#liuyanban").val().length;
		$("#liuyanTipsLeft").html(tipsLength);
	});
	
	// 点击备注
	$("#psb").click(function(){
		if($(this).is(":checked")){
			$("#ps").css("opacity","1");
		}else{
			$("#ps").css("opacity","0");
		}
	});
	
	// 鼠标悬浮到商品列表
	$(document).on("mouseenter",".shopListT-list>a",function(){
		$(this).css("color","#FF6A00");
	});
	$(document).on("mouseleave",".shopListT-list>a",function(){
		$(this).css("color","black");
	});
	/*
     * **************************************************************************
     * ********************                                  ********************
     * ********************      COPYRIGHT INFORMATION       ********************
     * ********************                                  ********************
     * **************************************************************************
     *                                                                          *
     *                                   _oo8oo_                                *
     *                                  o8888888o                               *
     *                                  88" . "88                               *
     *                                  (| -_- |)                               *
     *                                  0\  =  /0                               *
     *                                ___/'==='\___                             *
     *                              .' \\|     |// '.                           *
     *                             / \\|||  :  |||// \                          *
     *                            / _||||| -:- |||||_ \                         *
     *                           |   | \\\  -  /// |   |                        *
     *                           | \_|  ''\---/''  |_/ |                        *
     *                           \  .-\__  '-'  __/-.  /                        *
     *                         ___'. .'  /--.--\  '. .'___                      *
     *                      ."" '<  '.___\_<|>_/___.'  >' "".                   *
     *                     | | :  `- \`.:`\ _ /`:.`/ -`  : | |                  *
     *                     \  \ `-.   \_ __\ /__ _/   .-` /  /                  *
     *                 =====`-.____`.___ \_____/ ___.`____.-`=====              *
     *                                   `=---=`                                *
     * **************************************************************************
     * ********************                                  ********************
     * ********************      				 ********************
     * ********************         佛祖保佑 永远无BUG       ********************
     * ********************                                  ********************
     * **************************************************************************
     */
	// 点击提交按钮
	$("#pay-but").click(function(){
		// 点击提交 表单验证
		//获取地址行
		let addressRow =  $(".order-item-address-list .order-item-address-row").length;
		if(addressRow==0){
			alert("请先添加收获地址");
			return;
		}
		//获取选中的收获地址
		let addressRowActive =  $(".order-item-address-list .order-item-address-checkbox.checked").length;
		if(addressRowActive==0){
			alert("请选择收获地址");
			return;
		}
		//获取订货人姓名
		let dingName = $("#dingName").val();
		if(dingName==""){
			alert("请输入订货人姓名");
			return;
		}
		//获取订货人手机号
		let dingPhone = $("#dingShou").val();
		if(dingPhone==""){
			alert("请输入订货人手机号码");
			return;
		}
		if(!(/^1[3456789]\d{9}$/.test(dingPhone))){
			alert("请输入正确的订货人手机号码,手机号码必须为11位数字");
			return;
		}
		//获取送达日期
		let date = $("#riqi").val();
		if(date==""){
			alert("请选择送达日期");
			return;
		}
		//获取贺卡留言
		let liuYan = $("#liuyanban").val();
		//获取备注
		let beizhu = $("#ps").val();
		//如果没有勾选备注
		if(!$("#psb").prop("checked")){
			beizhu = "";
		}
		//获取应付金额
		let yingfu = $(".pay-price").html();
		//获取收获人姓名
		let shouName = $(".footer-rece .rece-name").html();
		//获取收获人手机号
		let shouPhone = $(".footer-rece .rece-phone").html();
		//获取收货人地址信息
		let shouAddress = $("#address").html();
		/**
		  编号 收获人姓名 收获手机号 收获地址 订货人姓名 订货手机号
		 	送达日期 贺卡留言 备注	*购物券编号* 应付金额 实付金额 订单状态 用户编号
		 */
		var objs = {
			"flag":"addOrder",
			"shouName":shouName,
			"shouPhone":shouPhone,
			"shouAddress":shouAddress,
			"dingName":dingName,
			"dingPhone":dingPhone,
			"date":date,
			"liuYan":liuYan,
			"beizhu":beizhu,
			"yingfu":yingfu,
			"shifu":yingfu
		};
		$.post("shoppingCart.shtml",objs,function(result){
			if(result=="true"){
				//提交订单成功
				location.href = "jieSuan.jsp";
			}else{
				alert("提交订单失败");
			}
		});

	});
});

//-----------
	$(function(){
		//-----点击新增默认地址 和 关闭--------
		$(".address-add").click(function(){
			$(".layer.add").show();
		});
		//点击其他阴影地方时 和点击取消和X号时 隐藏该窗口
		$(".layer.add .layer-shade").click(function(){
			$(".layer.add").hide();
		});
		$(".layer.add .layer-content-close").click(function(){
			$(".layer.add").hide();
		});
		$(".layer.add .layer_cancel").click(function(){
			$(".layer.add").hide();
		});
		//点击其他阴影地方时 和点击取消和X号时 隐藏该窗口  修改地址框
		$(".layer.update .layer-shade").click(function(){
			$(".layer.update").hide();
		});
		$(".layer.update .layer-content-close").click(function(){
			$(".layer.update").hide();
		});
		$(".layer.update .layer_cancel").click(function(){
			$(".layer.update").hide();
		});
		//悬浮出现设置地址修改 删除
		$(document).on("mouseover",".order-item-address-list .order-item-address-row",function () {
			//判断该调信息是不是默认地址
			var def = $(this).find(".order-item-address-default.yes").length;
			$(this).children(".order-item-address-options").css("display","inline-block");
			if(def==0){
				//不是默认地址
				$(this).children(".order-item-address-options").children(".order-item-address-setdef").show();
			}else{
				//是默认地址
				$(this).children(".order-item-address-options").children(".order-item-address-setdef").hide();
			}
			//判断 该条信息 是不是被选中的
			var sel = $(this).children(".order-item-address-checkbox.checked").length;
			if(sel==0){
				//没有被选中 显示删除
				$(this).children(".order-item-address-options").children(".order-item-address-delete").show();
			}else{
				//没有被选中 隐藏删除
				$(this).children(".order-item-address-options").children(".order-item-address-delete").hide();
			}
		});
		$(document).on("mouseout",".order-item-address-list .order-item-address-row",function () {
			$(this).children(".order-item-address-options").hide();
		});
		//点击设为默认地址
		$(document).on("click",".order-item-address-row .order-item-address-setdef",function () {
			var id = $(this).parents(".order-item-address-row").attr("dataid");
			//发送ajax请将该条地设置为默认地址
			$.post("address.shtml",{"flag":"setdefault","id":id},function(data){});

			$(this).parents(".order-item-address-row").siblings().find(".order-item-address-default.yes").removeClass("yes");
			$(this).parent().prev().children(":last").addClass("yes");
			$(this).hide();
		});
		//点击切换选中状态
		$(document).on("click",".order-item-address-row .order-item-address-checkbox",function () {
			$(this).parents(".order-item-address-row").siblings().children(".order-item-address-checkbox.checked").removeClass("checked");
			$(this).addClass("checked");
			//同是将带条地址信息绑定到页面下方 （配送至）
			//获取地址详细信息
			let addressContent = $(this).parent().find(".order-item-address-details").text();
			//获取收货人
			let addressName = $(this).parent().find(".order-item-address-name").text();
			//获取收获人手机号
			let addressPhone = $(this).parent().find(".order-item-address-tel").text();
			$("#address").text(addressContent);
			$(".footer-rece .rece-name").text(addressName);
			$(".footer-rece .rece-phone").text(addressPhone);
		});
		//点击删除 删除该条地址
		$(document).on("click",".order-item-address-row .order-item-address-delete",function () {
			//获取点击该条地址的编号
			var id = $(this).parents(".order-item-address-row").attr("dataid");
			if(confirm("确认要删除该条地址信息吗？")){
				//发送ajax将该条地址删除
				$.post("address.shtml",{"flag":"deleteAddress","id":id},function(data){});
				//将该条地址删除
				$(this).parents(".order-item-address-row").remove();
			}
		});
		//点击修改地址 （每一条的地址 都有的修改地址）
		$(document).on("click",".order-item-address-row .order-item-address-modify",function (){
			//获取点击该条地址的编号
			var id = $(this).parents(".order-item-address-row").attr("dataid");
			//发送请求到ajax查询该条地址
			$.post("address.shtml",{"flag":"selectById","id":id},function(data){
				data = eval("("+data+")");
				$(".layer.update input[name='toName']").val(data.shouName);
				$(".layer.update input[name='toPhone']").val(data.phone);
				$(".layer.update input[name='toAddressDetails']").val(data.addressContent);
				$(".j1s").val(data.province);
				var shi = data.city;
				var qu = data.area;
				$.ajax({
					url:"province.shtml",
					type:"post",
					data:{"pid":data.province},
					success:function(result){
						$(".j2s").empty();
						result = eval("("+result+")");
						$(result).each(function(i,e){
							$(".j2s").append("<option value="+e.id+">"+e.name+"</option>");
						});
						$(".j2s").val(shi);
					}
				});
				$.ajax({
					url:"province.shtml",
					type:"post",
					data:{"pid":shi},
					success:function(result){
						$(".j3s").empty();
						result = eval("("+result+")");
						$(result).each(function(i,e){
							$(".j3s").append("<option value="+e.id+">"+e.name+"</option>");
						});
						$(".j3s").val(qu);
					}
				});

			});
			//显示修改地址框
			$(".layer.update").show();
			//在修改地址框中 给上id属性值
			$(".layer.update").attr("dataid",id);
		});


	});
	//当页面加载时候发送ajax请求获取用户的地址信息
	$(function(){
		$.post("address.shtml",{"flag":"selectAllAddress"},function(data){
			var result = eval("("+data+")");
			//默认没有默认地址
			var ismoRen =  false;
			if(result.length==0){
				return;
			}
			//循环去当中找 如果有默认地址不用管 如果没有 则第一条地址被选中
			for(var m=0;m<result.length;m++){
				if(result[m].default){
					ismoRen = true;
					break;
				}
			}
			$(result).each(function(i,e){
				appendAddress(e);
			});
			if(!ismoRen){
				//如果没有默认地址 就让第一条被选中
				$(".order-item-address-list").children(":first").children(".order-item-address-checkbox").addClass("checked");
				//同是将带条地址信息绑定到页面下方 （配送至）
				//获取地址详细信息
				let addressContent = $(".order-item-address-list").children(":first").find(".order-item-address-details").text();
				//获取收货人
				let addressName = $(".order-item-address-list").children(":first").find(".order-item-address-name").text();
				//获取收获人手机号
				let addressPhone = $(".order-item-address-list").children(":first").find(".order-item-address-tel").text();
				$("#address").text(addressContent);
				$(".footer-rece .rece-name").text(addressName);
				$(".footer-rece .rece-phone").text(addressPhone);
			}
		});
	});
	//向 收获人信息中 追加新收获信息
function appendAddress(e){
	if(e.default){
		//是默认地址
		$(".order-item-address-list").append("\
			<div class='order-item-address-row' dataid='"+e.id+"'>\
				<span class='order-item-address-checkbox checked'>"+e.shouName+"\
					<img src='../image/loginImg/pcOrderAddrSelect.png'>\
				</span>\
			<div class='order-item-address-info'>\
				<span class='order-item-address-name'>"+e.shouName+"</span>\
				<span class='order-item-address-details'>"+e.sheng+e.shi+e.qu+e.addressContent+"</span>\
				<span class='order-item-address-tel'>"+e.phone+"</span>\
				<span class='order-item-address-default yes'>默认地址</span>\
			</div>\
			<div class='order-item-address-options'>\
				<span class='order-item-address-setdef'>设为默认地址</span>\
				<span class='order-item-address-modify'>修改</span>\
				<span class='order-item-address-delete'>删除</span>\
			</div>\
		</div>");
		//同是将带条地址信息绑定到页面下方 （配送至）
		$("#address").text(e.sheng+e.shi+e.qu+e.addressContent);
		$(".footer-rece .rece-name").text(e.shouName);
		$(".footer-rece .rece-phone").text(e.phone);
	}else{
		$(".order-item-address-list").append("\
			<div class='order-item-address-row' dataid='"+e.id+"'>\
				<span class='order-item-address-checkbox'>"+e.shouName+"\
					<img src='../image/loginImg/pcOrderAddrSelect.png'>\
				</span>\
			<div class='order-item-address-info'>\
				<span class='order-item-address-name'>"+e.shouName+"</span>\
				<span class='order-item-address-details'>"+e.sheng+e.shi+e.qu+e.addressContent+"</span>\
				<span class='order-item-address-tel'>"+e.phone+"</span>\
				<span class='order-item-address-default'>默认地址</span>\
			</div>\
			<div class='order-item-address-options'>\
				<span class='order-item-address-setdef'>设为默认地址</span>\
				<span class='order-item-address-modify'>修改</span>\
				<span class='order-item-address-delete'>删除</span>\
			</div>\
		</div>");
	}
}
	//点击添加默认地址
	$(function(){
		$(".layer.add .layer_add").click(function(){
			//获取姓名 手机号 详细地址
			var name = $(".layer.add input[name='toName']").val();
			var phone = $(".layer.add input[name='toPhone']").val();
			var addressDetails = $(".layer.add input[name='toAddressDetails']").val();
			var rge = /^1[34578][0-9]{9}$/;//手机号正则表达式
			if(name==""){
				alert("请输入收获人姓名");
				return;
			}
			if(phone==""){
				alert("请输入电话号码");
				return;
			}
			if(addressDetails==""){
				alert("请输入详细地址");
				return;
			}
			if(!rge.test(phone)){
				alert("手机号输入有误");
				return;
			}
			var sheng = $(".j1").val();
			var shi = $(".j2").val();
			var qu = $(".j3").val();
			if(shi==undefined){
				shi = 222;
			}
			if(qu==undefined){
				qu=222;
			}
			var obj = {
					"flag":"insertAddress",
					"name":name,
					"phone":phone,
					"addressDetails":addressDetails,
					"j1":sheng,
					"j2":shi,
					"j3":qu
					}
			//发送ajax请求添加地址信息
			$.post("address.shtml",obj,function(d){
				if(d!=""){
					alert("添加地址成功");
					//将新地址追加到 收获人信息中
					appendAddress(eval("("+d+")"));
				}
			});
			//清空输入的值
			$(".layer.add input[name='toName']").val("");
			$(".layer.add input[name='toPhone']").val("");
			$(".layer.add input[name='toAddressDetails']").val("");
			//最后隐藏掉添加框子
			$(".layer.add").hide();
		});
	});
//新增地址三级联动
$(function(){
	//加载页面发送ajax获取地址
	$.ajax({
		url:"province.shtml",
		type:"post",
		data:{"pid":0},
		success:function(result){
			result = eval("("+result+")");
			$(result).each(function(i,e){
				$(".j1").append("<option value="+e.id+">"+e.name+"</option>");
				$(".j1s").append("<option value="+e.id+">"+e.name+"</option>");
			});
		}
	});
	$(".j1").change(function(){
		var pid = $(this).val();
		$.ajax({
			url:"province.shtml",
			type:"post",
			data:{"pid":pid},
			success:function(result){
				$(".j2").empty();
				result = eval("("+result+")");
				$(result).each(function(i,e){
					$(".j2").append("<option value="+e.id+">"+e.name+"</option>");
				});
				$(".j2").change();
			}
		});
	});
	$(".j2").change(function(){
		var pid = $(this).val();
		$.ajax({
			url:"province.shtml",
			type:"post",
			data:{"pid":pid},
			success:function(result){
				$(".j3").empty();
				result = eval("("+result+")");
				$(result).each(function(i,e){
					$(".j3").append("<option value="+e.id+">"+e.name+"</option>");
				});
			}
		});
	});
	$(".j1s").change(function(){
		var pid = $(this).val();
		$.ajax({
			url:"province.shtml",
			type:"post",
			data:{"pid":pid},
			success:function(result){
				$(".j2s").empty();
				result = eval("("+result+")");
				$(result).each(function(i,e){
					$(".j2s").append("<option value="+e.id+">"+e.name+"</option>");
				});
				$(".j2s").change();
			}
		});
	});
	$(".j2s").change(function(){
		var pid = $(this).val();
		$.ajax({
			url:"province.shtml",
			type:"post",
			data:{"pid":pid},
			success:function(result){
				$(".j3s").empty();
				result = eval("("+result+")");
				$(result).each(function(i,e){
					$(".j3s").append("<option value="+e.id+">"+e.name+"</option>");
				});
			}
		});
	});
});
//点击修改默认地址
$(function(){
	$(".layer.update .layer_add").click(function(){
		//获取要修改的编号
		var id = $(".layer.update").attr("dataid");
		//获取姓名 手机号 详细地址
		var name = $(".layer.update input[name='toName']").val();
		var phone = $(".layer.update input[name='toPhone']").val();
		var addressDetails = $(".layer.update input[name='toAddressDetails']").val();
		var rge = /^1[34578][0-9]{9}$/;//手机号正则表达式
		if(name==""){
			alert("请输入收获人姓名");
			return;
		}
		if(phone==""){
			alert("请输入电话号码");
			return;
		}
		if(addressDetails==""){
			alert("请输入详细地址");
			return;
		}
		if(!rge.test(phone)){
			alert("手机号输入有误");
			return;
		}
		var sheng = $(".j1s").val();
		var shi = $(".j2s").val();
		var qu = $(".j3s").val();
		if(shi==undefined){
			shi = 222;
		}
		if(qu==undefined){
			qu=222;
		}
		var obj = {
			"flag":"updateAddress",
			"id":id,
			"name":name,
			"phone":phone,
			"addressDetails":addressDetails,
			"j1":sheng,
			"j2":shi,
			"j3":qu
		}
		//发送ajax请求添加地址信息
		$.post("address.shtml",obj,function(d){
			if(d!=""){
				alert("修改地址成功");
				var obj = eval("("+d+")");
				//修改掉原来的值
				$(".order-item-address-row[dataid="+id+"]").children(".order-item-address-checkbox").text(obj.shouName);
				$(".order-item-address-row[dataid="+id+"]").find(".order-item-address-name").text(obj.shouName);
				$(".order-item-address-row[dataid="+id+"]").find(".order-item-address-details").text(obj.sheng+obj.shi+obj.qu+obj.addressContent);
				$(".order-item-address-row[dataid="+id+"]").find(".order-item-address-tel").text(obj.phone);
			}
		});
		//最后隐藏掉修改框子
		$(".layer.update").hide();
	});
});
//----加载购物车中被选中的商品
$(function(){
	$.post("shoppingCart.shtml",{"flag":"selectActive"},function (data){
		data = eval("("+data+")");
		//计算商品的总价格 和总件数
		let totalPrice= 0;
		let totalCount = 0;
		$(data).each(function(i,e){
			//商品价格
			totalPrice += e.num*e.commodity.outPrice;
			//商品件数
			totalCount += e.num;
			$(".shopListT").append("\
				<div class='shopListT-list'>\
					<a href='' style='display: inline-block;width: 100%;height: 100%;color: #232323;'>\
						<div class='list-pic'>\
							<img src='../image/commodity/"+e.commodity.comImage+"' >\
						</div>\
						<div class='list-info'>\
							<p class='list-ps'>"+e.commodity.comName+"</p>\
							<div>\
								<span class='list-price'>"+e.commodity.outPrice+"</span>\
								<span class='list-count'>x"+e.num+"</span>\
							</div>\
						</div>\
					</a>\
				</div>");
			$("#sumMoney").html(totalPrice);
			$(".pay-price").html(totalPrice);
			$("#list-count").html(totalCount);
		});
	});

});
/* 商品列表模板
    * <div class="shopListT-list">
    <a href="" style="display: inline-block;width: 100%;height: 100%;color: #232323;">
        <div class="list-pic">
            <img src="img/1065046.jpg_220x240.jpg" >
        </div>
        <div class="list-info">
            <p class="list-ps">Best for You-戴安娜粉玫瑰11枝</p>
            <div>
                <span class="list-price">145</span>
                <span class="list-count">x1</span>
            </div>
        </div>
    </a>
</div>
    *
    * */